package com.engine.salary.service.impl;

import com.engine.core.impl.Service;
import com.engine.salary.constant.SalaryDefaultTenantConstant;
import com.engine.salary.entity.taxagent.po.TaxAgentAdminPO;
import com.engine.salary.mapper.taxagent.TaxAgentAdminMapper;
import com.engine.salary.service.TaxAgentAdminService;
import com.engine.salary.util.db.MapperProxyFactory;
import com.google.common.collect.Lists;
import com.engine.salary.util.db.IdGenerator;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 个税扣缴义务人管理员
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class TaxAgentAdminServiceImpl extends Service implements TaxAgentAdminService {

    private TaxAgentAdminMapper getTaxAgentAdminMapper() {
        return MapperProxyFactory.getProxy(TaxAgentAdminMapper.class);
    }

    @Override
    public void deleteByTaxAgentIds(Collection<Long> taxAgentIds) {
        if (CollectionUtils.isEmpty(taxAgentIds)) {
            return;
        }
        getTaxAgentAdminMapper().deleteByTaxAgentIds(taxAgentIds);
    }

    @Override
    public void batchInsert(Long taxAgentId, Collection<Long> admins) {
        if (taxAgentId == null || CollectionUtils.isEmpty(admins)) {
            return;
        }
        Date now = new Date();
        admins.stream()
                .map(e -> TaxAgentAdminPO.builder()
                        .id(IdGenerator.generate())
                        .taxAgentId(taxAgentId)
                        .employeeId(e)
                        .createTime(now)
                        .updateTime(now)
                        .deleteType(0)
                        .creator((long) user.getUID())
                        .tenantKey(SalaryDefaultTenantConstant.DEFAULT_TENANT_KEY)
                        .build())
                .forEach(getTaxAgentAdminMapper()::insertIgnoreNull);
    }

    @Override
    public List<TaxAgentAdminPO> listByTaxAgentIds(Collection<Long> taxAgentIds) {
        List<TaxAgentAdminPO> list = Lists.newArrayList();
        if (CollectionUtils.isEmpty(taxAgentIds)) {
            return list;
        }
        return getTaxAgentAdminMapper().listSome(TaxAgentAdminPO.builder().taxAgentIds(taxAgentIds).build());

    }

    @Override
    public List<TaxAgentAdminPO> listByTaxAgentIdsAndEmployeeId(Collection<Long> taxAgentIds, Long currentEmployeeId) {
        List<TaxAgentAdminPO> list = Lists.newArrayList();
        if (CollectionUtils.isEmpty(taxAgentIds) || currentEmployeeId == null) {
            return list;
        }
        return getTaxAgentAdminMapper().listSome(TaxAgentAdminPO.builder().taxAgentIds(taxAgentIds).employeeId(currentEmployeeId).build());
    }

    @Override
    public List<TaxAgentAdminPO> listByEmployeeId(Long currentEmployeeId) {
        return getTaxAgentAdminMapper().listSome(TaxAgentAdminPO.builder().employeeId(currentEmployeeId).build());
    }

    @Override
    public List<Long> getAdminTaxAgentIds(Long currentEmployeeId) {
        return listByEmployeeId(currentEmployeeId).stream().map(TaxAgentAdminPO::getTaxAgentId).collect(Collectors.toList());
    }
}
